package com.peteralbus.service.impl;

import com.peteralbus.entity.EarthquakeInfo;
import com.peteralbus.entity.FireCenter;
import com.peteralbus.entity.Hospital;
import com.peteralbus.entity.IntensityLine;
import com.peteralbus.mapper.FireCenterMapper;
import com.peteralbus.service.EarthquakeInfoService;
import com.peteralbus.service.FireCenterService;
import com.peteralbus.service.IntensityLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FireCenterServiceImpl implements FireCenterService {

    FireCenterMapper fireCenterMapper;
    EarthquakeInfoService earthquakeInfoService;
    IntensityLineService intensityLineService;
@Autowired
    public void setIntensityLineService(IntensityLineService intensityLineService) {
        this.intensityLineService = intensityLineService;
    }

    @Autowired
    public void setEarthquakeInfoService(EarthquakeInfoService earthquakeInfoService) {
        this.earthquakeInfoService = earthquakeInfoService;
    }

    @Autowired
    public void setFireCenterMapper(FireCenterMapper fireCenterMapper) {
        this.fireCenterMapper = fireCenterMapper;
    }

    @Override
    public List<FireCenter> getAllFireCenter() {
        return fireCenterMapper.selectList(null);
    }
    @Override
    public   List<FireCenter> getAllNearFireCenter(Double lon,Double lat,Double lonDistance,Double latDistance){
        List<FireCenter> fireCenterst=new ArrayList<>();
        int i=1;
        //防止找不到医院，依次扩大范围
        while (fireCenterst.size()==0){
            fireCenterst=fireCenterMapper.getAllNearFireCenter(lon, lat, lonDistance*i, latDistance*i);
            i++;
        }
        return fireCenterst;
    }
    @Override
    public List<FireCenter> getAllNearFireCenterById(long id){
        EarthquakeInfo earthquakeInfo = earthquakeInfoService.getEarthquakeInfoById(id);
        List<IntensityLine> list = intensityLineService.getIntensityLineByEarthquakeId(id);
        if (list.size() == 0)
            return getAllNearFireCenter(earthquakeInfo.getLongitude(), earthquakeInfo.getLatitude(), 1.0, 1.0);
        else {
            Double lonMax = 0.0, latMax = 0.0;
            for (IntensityLine intensityLine : list) {
                if (intensityLine.getLongRadius() > lonMax) lonMax = intensityLine.getLongRadius()/111;
                if (intensityLine.getShortRadius() > latMax) latMax = intensityLine.getShortRadius()/111;
            }
            return getAllNearFireCenter(earthquakeInfo.getLongitude(), earthquakeInfo.getLatitude(), lonMax, latMax);
        }
    }

}
